import Creator from './demo_tree_data';
import { DemoTreeConst } from 'src/consts/demo_tree_const';
import { Context } from 'vm';
import { DemoApi } from 'src/api';
import { addTreeChildren, findTreeNode } from "src/utils/tree_utils";
const state: any = Creator.dataCreator();

const actions: any = {
    [DemoTreeConst.GetOrgTreeData](context: VuexData, data?: any): void {
        DemoApi.ajaxTreeData(-1, (res: any) => {
            WLCommit(DemoTreeConst.UpdateOrgTreeData, res.data);
        });
    },
    [DemoTreeConst.ExpandTree](context: VuexData, data?: any): void {
        DemoApi.ajaxTreeData(data.id, (res: any) => {
            var param: any = {};
            param.id = data.id;
            param.data = res.data;
            WLCommit(DemoTreeConst.AddTreeNode, param);
        });
    }
}

const mutations: any = {
    /** 添加展开节点 */
    [DemoTreeConst.AddTreeNode](state: any, data: any): void {
        let row: any = findTreeNode(state.data, data.id);
        console.log("row", row, "id", data.id);
        if (row) {
            addTreeChildren(row, data.id, data.data);
        } else {

        }
    },

    /** 初始化列表 */
    [DemoTreeConst.UpdateOrgTreeData](state: any, data: any): void {
        state.data = data;
    },

}

export default {
    state,
    actions,
    mutations,
}